<?php

  
defined( '_VALID_MOS' ) or die( 'Restricted access' );

/**
* installation function
*/
// ************************************************************************
function com_install( ) 
{
 	global $database, $mainframe;

	// update component mini-image
	$query = " UPDATE #__components "
			." SET `admin_menu_img` = '../components/com_helpdesk/images/helpdesk_16.png' "
			." WHERE `admin_menu_link` = 'option=com_helpdesk' "
			;
	$database->setQuery($query);
	if ( !$database->query() ){  
		echo $database->getErrorMsg()."<br />";
	}
	
	// populate addons with lang and mods
	$database->setQuery(" SELECT * FROM #__helpdesk_addons WHERE `type` = 'lang' AND `filename` = 'english' ");	
	$database->loadObject($english);
	if (!$english) {
		$database->setQuery("INSERT INTO #__helpdesk_addons VALUES('', 'lang', 'English Language File', 'English definitions of constants', 'english', 1, 0, 0, 1, 0, '0000-00-00 00:00:00', '0.1', '2008-03-08 09:30:15', 'Dioscouri Design', 'info@dioscouri.com', 'http://www.dioscouri.com')");
		if ( !$database->query() ){  
			echo $database->getErrorMsg()."<br />";
		}
	}

		$table = '#__helpdesk_tickets';
		$field = 'categoryid';
		$definition = 'INT(11) NOT NULL';
		
		// if the field doesn't exist, insert it
			$query = " SHOW COLUMNS FROM ".$table. "\n LIKE '".$field."' ";
			$database->setQuery( $query );
			$rows = $database->loadObjectList();
			if ($database->getErrorNum()) {
				echo $database->getErrorMsg();
			}
		
			if (!$rows && !$database->getErrorNum()) {
				
				echo "<p style='color:#FFCC00;'>No column of the name `".$field."` in `".$table."`</p>";
				
				$query = "ALTER TABLE ".$table." ADD `".$field."` ".$definition.";";
				$database->setQuery( $query );
				if (!$database->query()) {
					echo $database->stderr();
					echo "<p style='color:#FFCC00;'>Failed to create `".$field."` in `".$table."`</p>";
				} else {
					echo "<p style='color:#00CC00;' >Column `".$field."` created in `".$table."` </p>";			
				}
				
			}		

}

?>